package com.workcell.workdata.im.component.validator;

import com.workcell.workdata.im.annotation.validator.MessageVideoFormat;
import com.workcell.workdata.im.constant.MessageFormat;
import com.workcell.workdata.im.model.message.CloudVideo;
import com.workcell.workdata.im.model.request.MessageRequest;
import com.workcell.workdata.im.util.JSON;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class MessageVideoValidator implements ConstraintValidator<MessageVideoFormat, MessageRequest> {

    @Override
    public boolean isValid(MessageRequest momentRequest, ConstraintValidatorContext context) {

        if (momentRequest.getFormat() != MessageFormat.VIDEO){
            return true;
        }

        CloudVideo video = JSON.parseNullable(momentRequest.getContent(), CloudVideo.class);

        return video != null
                && video.getVideo() != null
                && video.getImage() != null
                && video.getBucket() != null;
    }
}
